package com.yorick.file.controller;

import com.yorick.file.modal.request.DeleteFileRequest;
import com.yorick.file.modal.request.RestoreFileRequest;
import com.yorick.file.service.BinService;
import com.yorick.login.enums.Account;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.Map;

@Slf4j
@RestController
@RequestMapping("bin")
public class BinController {
    @Autowired
    BinService binService;

    @PostMapping(value = "fileList")
    public Map<String, Object> fileList(HttpServletRequest request){
        HttpSession session = request.getSession();
        int aid = Integer.parseInt(session.getAttribute(Account.aid.name()).toString());
        return binService.fileList(aid);
    }

    @PostMapping(value = "deleteFile")
    public Map<String, Object> deleteFile(@RequestBody DeleteFileRequest requestBody, HttpServletRequest request){
        HttpSession session = request.getSession();
        int aid = Integer.parseInt(session.getAttribute(Account.aid.name()).toString());
        return binService.deleteFile(requestBody.getFileId(), aid);
    }

    @PostMapping(value = "restoreFile")
    public Map<String, Object> restoreFile(@RequestBody RestoreFileRequest requestBody, HttpServletRequest request){
        HttpSession session = request.getSession();
        int aid = Integer.parseInt(session.getAttribute(Account.aid.name()).toString());
        return binService.restoreFile(requestBody.getFileId(), aid);
    }
}
